package com.deelete.springcloud.controller;

import com.deelete.springcloud.pojo.Storage;
import com.deelete.springcloud.service.StorageService;
import com.deelete.springcloud.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class StorageController {
    @Resource
    private StorageService storageService;

    @RequestMapping("/storage/createStorage")
    public CommonResult<Storage> createStorage(@RequestParam("productId") Long productId,@RequestParam("count") Integer count){
        CommonResult<Storage> result = new CommonResult<>();
        if (productId==0L || count == 0){
            result.setStatus(CommonResult.STATUS_ERROR);
            result.setMessage("订单为空");
        }else{
            //判断库存是否扣减成功
            Boolean storage = storageService.createStorage(productId, count);
            if(storage){
                result.setStatus(CommonResult.STATUS_SUCCESS);
                result.setMessage("库存增减成功");
            }else {
                result.setStatus(CommonResult.STATUS_ERROR);
                result.setMessage("库存增减失败");
            }
        }
        return result;
    }
}
